package com.myworkbench.workbench.domain.entity.user;

import com.myworkbench.workbench.domain.Command;
import com.myworkbench.workbench.domain.entity.role.Role;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaDelete;
import java.awt.print.Book;
import java.io.Serializable;

@Entity
@Table(name = "rel_user_role")
@AllArgsConstructor
@NoArgsConstructor
public class RelUserRole extends Command implements Serializable {
    @EmbeddedId
    @ManyToOne(cascade = CascadeType.REFRESH)
    @JoinColumn(name = "USER_ID")
    private User user;
    @EmbeddedId
    @ManyToOne(cascade = CascadeType.REFRESH)
    @JoinColumn(name = "ROLE_ID")
    private Role role;


    /**
     * 解除所有权限
     */
    public  void revokeAllPrivileges(Integer id){
        CriteriaBuilder cb =getRepository().getCriteriaBuilder();
        CriteriaDelete<RelUserRole> delete = cb.createCriteriaDelete(RelUserRole.class);
        delete.from(RelUserRole.class);
        // 批量删除
        delete.where(cb.equal(delete.getRoot().get("user").get("id"),id));
        Query query = getRepository().createQuery(delete);
        query.executeUpdate();
    }

}
